package org.example.integration.adapter.customer;

import org.example.cache.core.CacheScope;
import org.example.cache.stereotype.CachingMethod;
import org.example.cache.stereotype.MultiLayerCaching;
import org.example.customer.api.CustomerService;
import org.example.customer.api.SdkCustomerDTO;
import org.example.order.domain.Customer;
import org.example.order.domain.CustomerRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@MultiLayerCaching(cacheName = "CUSTOMER", scope = CacheScope.REQUEST)
public class CustomerAdapter implements CustomerRepository {
    private static final Logger logger = LoggerFactory.getLogger(CustomerAdapter.class);
    private CustomerService customerService;

    public CustomerAdapter(@Autowired CustomerService customerService) {
        this.customerService = customerService;
    }

    @CachingMethod()
    @Override
    public Customer getCustomer(String customerId) {
        SdkCustomerDTO sdkCustomerDTO = customerService.getCustomer(customerId);
        if (logger.isInfoEnabled()) {
            logger.info("\uD83D\uDCBB RPC for Customer, key: {}", customerId);
        }
        return CustomerConverter.INSTANCE.convert(sdkCustomerDTO);
    }

    @CachingMethod()
    @Override
    public List<Customer> getCustomers(List<String> customerIds) {
        List<SdkCustomerDTO> orgDTOList = customerService.getCustomers(customerIds);
        if (logger.isInfoEnabled()) {
            logger.info("\uD83D\uDCBB RPC for Customers, keys: {}", String.join(",", customerIds));
        }
        return CustomerConverter.INSTANCE.convert(orgDTOList);
    }
}
